<?php
/* *******************************************
 * Copied Right Reserved (c) Compass Technology *
 * Description: Coodinator of the web    *
 * Author: Cavin Zhang @ Feb 25,2013            *
* ********************************************/

@session_start();

include '../classes/os.constants.ini.php';
include '../classes/class.errorEventLog.php';
include '../classes/class.PDODatabaseOpera.php';
include '../classes/generalFunc.php';
include '../classes/Mobile_Detect.php';
//create a new DB Object
$dbObj = new PDODatabaseOpera (DB_HOSTNAME, DB_USERNAME, DB_PASSWORD,DB_INSTANCE_NAME);

if(isset($_GET['opt'])){
	
	$optFilePath = '../main/'.PREDFINED_PREFIX.$_GET['opt'].'.php';
	
	if(@file_exists($optFilePath)){
		include $optFilePath;
	}else{
		errorEventLog::errLogToFile('OPER_EXCEP', 'No such file exists :'.$optFilePath);
		die();
	}
	
}else{
	include '../phplib/php/template.inc';
	$zTemplate = new Template ();
	$interfacePrinter = array('domainPath'=>WELCOME_COMPASS_HOME);
	$zTemplate->set_file ( 'main', '../html/esyparking_mobile.html' );
	
	/********************************************
	 * Cavin @ Mar 15,2013
	 * This is the entry for verification steps so that all the visitors are in contorl
	 * a valid url appended in the app should be sth like "url + devid=*****&userkey=*****"
	 * @ Mobile Device Application need to pass two params in order to access to our web application
	 * @ Any of those which are manually input address or URL in the mobiles' browsers will be treated as invalid source and 
	 *   will be redirect to the relevant app store to download our app 
	 * @ PC visiting is allowed at current stage
	 */
	$detect = new Mobile_Detect;
	
	if ($detect->isMobile() || $detect->isTablet()){
		//errorEventLog::displaysSQLPhase('firstly open the web','This is the mobile device!');
		//detect as a mobile device including the tablet
		
		
		
		if(!isset($_GET['devid'])||$_GET['devid']==''){
			
			if(isset($_GET['key'])&&$_GET['key']!=''){
				$interfacePrinter['userKey'] = $_GET['key'];
				$sql = 'SELECT `key` FROM `'.GET_ACCESS_KEY_TABLE.'` WHERE `id` = "'.$_GET['devid'].'"';
				$tRecordSet = $dbObj -> getOneQuery($sql);
				errorEventLog::displaysSQLPhase($sql,'Get the User Key');
				if($tRecordSet){
						
				}
			
			}else{
				//NO NEED TO FECTH HISTORY OF THE SEARCHED KEYWORD 
				$interfacePrinter['userKey'] = 'N';
			}
		}else{
			//REJECT VISITING TO APPEND HERE,USUALLY THE APP STORE LINK AND ANY OTHER ANDROID DOWNLOAD PAGE
			$interfacePrinter['userKey'] = 'N';
		}
		
		
	}else{
		//THE PC HANDLING
		$interfacePrinter['userKey'] = 'N';
	}
	
	
	
	/**
	 * Check whehter it is the PH today
	 */
	$today = date('Ymd',time());
	$sql = 'SELECT `timePeriod` FROM `saholidays` WHERE `holiday` ="'.$today.'"';
	$tRecordSet = $dbObj ->getOneQuery($sql);
	if($tRecordSet){
		$interfacePrinter['phFlag'] = '8'.$tRecordSet['TIMEPERIOD']; //8 FOR PH
	}else{
		$interfacePrinter['phFlag'] = 'N';
	}
	
	
	$feeType = array('1'=>'FREE','2'=>'FEE','3'=>'ALL');
	$parkingTime = array('10'=>'10mins','15'=>'15mins','20'=>'20mins','30'=>'30mins','45'=>'45mins','60'=>'1hr','120'=>'2hrs','180'=>'> 3hrs');
	$feeTypeOption='';
	foreach ($feeType as $key => $val)
	{
		$feeTypeOption .='<option value="'.$key.'">'.$val.'</option>';
	}
	$interfacePrinter['feeType'] = $feeTypeOption;
	
	$feeTypeOption='';
	//deal with the time selection
	for($i = 0; $i <=24;$i++)
	{
		$j = ($i<10)?'0'.$i:$i;
		$feeTypeOption .= "<option value='$i' id='hrs-$i'>".$j."</option>";
	}
	$interfacePrinter['hourType'] = $feeTypeOption;
	
	$feeTypeOption='';
	for($i = 0; $i <=59;$i++)
	{
		$j = ($i<10)?'0'.$i:$i;
		$feeTypeOption .= "<option value='$j' id='min-$i'>".$j."</option>";
	}
	$interfacePrinter['ver'] = md5(time());
	$interfacePrinter['minType'] = $feeTypeOption;
	
	$feeTypeOption='';
	foreach ($parkingTime as $key => $val)
	{
		$feeTypeOption .= "<option value='$key'>".$val."</option>";
	}
	$interfacePrinter['Parktime'] = $feeTypeOption;
	
	$zTemplate->set_var ($interfacePrinter);
	$zTemplate->parse ( 'mains', 'main' );
	$zTemplate->p ( 'mains' );
}




